package com.musifind.utils;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class FavouritesDatabase {

	private static final String DATABASE_NAME = "favourites";
	private static final String FAVS_TABLE = "favs";

	private static final int DATABASE_VERSION = 1;

	private final Context context;
	private FavouritesHelper dbHelper;
	public SQLiteDatabase sqLiteDatabase;

	// verilerim tablosuna ait sutunlar
	private static final String KEY_ROW_ID = "_id";
	private static final String KEY_TRACK_ID = "trackId";
	private static final String KEY_TRACK_URL = "trackURL";
	private static final String KEY_COVER_IMAGE = "coverImage";
	private static final String KEY_TRACK_NAME = "trackName";
	private static final String KEY_ARTIST_NAME = "artistName";
	private static final String KEY_ALBUM_NAME = "albumName";

	public FavouritesDatabase(Context c) {

		this.context = c;
		openConnection();

	}

	public FavouritesDatabase openConnection() {
		dbHelper = new FavouritesHelper(context);
		sqLiteDatabase = dbHelper.getWritableDatabase();
		return this;
	}

	public void closeConnection() {
		dbHelper.close();
	}

	private static class FavouritesHelper extends SQLiteOpenHelper {
		public FavouritesHelper(Context context) {
			super(context, DATABASE_NAME, null, DATABASE_VERSION);
		}

		@Override
		public void onCreate(SQLiteDatabase db) {

			String sqlCreateTableFavs = "CREATE TABLE [favs] ("
					+ "  [_id] INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, " + "  [trackId] TEXT, "
					+ "  [trackURL] TEXT, " + "  [coverImage] TEXT, " + "  [trackName] TEXT, "
					+ "  [artistName] TEXT, " + "  [albumName] TEXT)";

			db.execSQL(sqlCreateTableFavs);

		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			onCreate(db);

		}

	}

	public Cursor selectQuery(String query) {
		Cursor c1 = null;
		try {

			if (sqLiteDatabase.isOpen()) {
				sqLiteDatabase.close();

			}
			sqLiteDatabase = dbHelper.getReadableDatabase();
			c1 = sqLiteDatabase.rawQuery(query, null);

		} catch (Exception e) {
			Log.e("DATABASE ERROR ", "db error");

		}
		return c1;

	}

	public void saveFavourite(String trackID, String trackURL, String coverImage, String trackName, String artistName,
			String albumName) {
		ContentValues cv = new ContentValues();
		cv.put(KEY_TRACK_ID, trackID);
		cv.put(KEY_TRACK_URL, trackURL);
		cv.put(KEY_COVER_IMAGE, coverImage);
		cv.put(KEY_TRACK_NAME, trackName);
		cv.put(KEY_ARTIST_NAME, artistName);
		cv.put(KEY_ALBUM_NAME, albumName);

		if (!sqLiteDatabase.isOpen())
			openConnection();
		sqLiteDatabase.insert(FAVS_TABLE, null, cv);
		sqLiteDatabase.close();
	}

	public boolean deleteFavourite(String trackId) {
		if (!sqLiteDatabase.isOpen())
			openConnection();
		return sqLiteDatabase.delete(FAVS_TABLE, KEY_TRACK_ID + "=" + trackId, null) > 0;
	}

	public boolean isFavourite(String trackId) {
		if (!sqLiteDatabase.isOpen())
			openConnection();
		Cursor cursor = sqLiteDatabase.rawQuery("select 1 from favs where trackId=?", new String[] { trackId });
		boolean exists = (cursor.getCount() > 0);
		cursor.close();
		sqLiteDatabase.close();
		return exists;
	}

}
